package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        //    查询是否关联菜品
        Integer integer = dishMapper.selectCount(lqw);
        if (integer > 0) {
            throw new CustomException("当前分类下关联了菜品，不能删除");
            //return R.error("关联菜品，删除失败");

        }
        //    查询是否关联套餐
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId,id);
        Integer integer1 = setmealMapper.selectCount(lqw1);
        if (integer1 > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除");
            //return R.error("关联套餐,删除失败");
        }
        //返回结果
        int deleteById = categoryMapper.deleteById(id);
        //return deleteById>0?R.success("删除成功"):R.error("删除失败");
    }
}
